 <?php 
 error_reporting(0);
 
require_once("php/database.include.php");

echo $_SERVER['QUERY_STRING'] .  '<br/>';

$groupName = $_GET['groupName'];
$groupPublicKey = $_GET['groupPublicKey'];
$encryptedGroupPrivateKey = $_GET['encryptedGroupPrivateKey'];
$groupKeyModulus = $_GET['groupKeyModulus'];
$groupDescription = $_GET['groupDescription'];

//echo 'groupName: ' . $groupName 
//	. '<br/>groupPublicKey: ' . $groupPublicKey 
//	. '<br/>encryptedGroupPrivateKey: ' . $encryptedGroupPrivateKey 
//	. '<br/>groupKeyModulus: ' . $groupKeyModulus 
//	. '<br/>groupDescription: ' . $groupDescription 
//	. '<br/>';

//debug only

//first time through don't attempt validation

if (empty($groupName) && empty($groupPublicKey) && empty($encryptedGroupPrivateKey) && empty($groupKeyModulus) && empty($groupDescription) ) {
	echo 'no data';
	exit;
}

$inputErrors = validateGroupInput($groupName, $groupPublicKey, $encryptedGroupPrivateKey, $groupKeyModulus, $groupDescription);

if (strcmp('', $inputErrors) == 0 ) {
	//echo 'calling createGroup';
	return createGroup ($conn, $groupName, $groupPublicKey, $encryptedGroupPrivateKey, $groupKeyModulus, $groupDescription);
}
else {
	echo  "Create%20group%20failed.%20" . $inputErrors;
	return ("Create%20group%20failed.%20" . $inputErrors);
}

function validateGroupInput($user, $pass, $key) {
	$retVal = '';
	if (empty($user)) {
		$retVal .= 'Username%20cannot%20be%20mpty .';
	}
	if (empty($pass)) {
		$retVal .= 'Password%20cannot%20be%20empty .';
	}
	if (empty($key)) {
		$retVal .= 'Public%20key%20cannot%20be%20empty .';
	}
	
	return $retVal;
}


/**  $groupName, $groupPublicKey, $groupDescription
* Registers a new user for the site. If unable to register the user or if the username already
* exists, the user is given an error message and may attempt to register again.
* 
* @param groupName String The new group's name (unencrypted)
* @param groupPublicKey String The new groupr's 1024 bit public RSA key (unencrypted). 
* @param groupDescription String The new group's description

* @returns on failure, failure message explaining error. 
*          on success, success message
*/
function createGroup( $conn, $groupName, $groupPublicKey, $encryptedGroupPrivateKey, $groupKeyModulus, $groupDescription ) {

	$conn->beginTransaction();
	try {
		//Create new group		
		$stmt = $conn->prepare(' INSERT INTO `group` (name, public_key, group_key_modulus, description) VALUES (?, ?, ?, ?) ');
		$createGroupSuccess = $stmt->execute( array( $groupName, $groupPublicKey, $groupKeyModulus, $groupDescription ) );		
		$stmt = $conn->prepare('INSERT INTO `group_user` (group_name, user_name, encrypted_group_private_key) VALUES(?, ?, ?)');
		$insertGroupUserSuccess = $stmt->execute( array( $groupName, $_SESSION['username'], $encryptedGroupPrivateKey ) );
		$conn->commit();
		echo "<br/>Success on creating group: " . $groupName;
		return "<br/>Success on creating group: " . $groupName;
	}	
	catch(PDOException $err) {
		$conn->rollBack();
		echo "<br/>Failure creating group: " . $groupName . ". Duplicate group.";		
		return "<br/>Failure creating group: " . $groupName . ". Duplicate group.";
	}
	catch(Exception $err) 
	{
		$conn->rollBack();
		echo "<br/>Internal failure creating group: " . $groupName . ".";
		return "Internal failure creating group: " . $groupName . ".";
	}}
?>
